home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ für Kids
/
C++ for kids.iso
/
SETUP
/
US
/
CBUILDER
/
DATA.Z
/
LIST.H
< prev
next >
Wrap
C/C++ Source or Header
|
1997-02-13
|
24KB
|
840 lines
#ifndef __STD_LIST__
#define __STD_LIST__
/* $Revision: 8.1 $ */
/***************************************************************************
*
* list - list declarations for the Standard Library
*
* $Id: list,v 1.47 1995/09/18 23:31:35 lijewski Exp $
*
***************************************************************************
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
***************************************************************************
*
* (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
* ALL RIGHTS RESERVED
*
* The software and information contained herein are proprietary to, and
* comprise valuable trade secrets of, Rogue Wave Software, Inc., which
* intends to preserve as trade secrets such software and information.
* This software is furnished pursuant to a written license agreement and
* may be used, copied, transmitted, and stored only in accordance with
* the terms of such license and with the inclusion of the above copyright
* notice. This software and information or any other copies thereof may
* not be provided or otherwise made available to any other person.
*
* Notwithstanding any other lease or license that may pertain to, or
* accompany the delivery of, this computer software and information, the
* rights of the Government regarding its use, reproduction and disclosure
* are as set forth in Section 52.227-19 of the FARS Computer
* Software-Restricted Rights clause.
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7013.
* Contractor/Manufacturer is Rogue Wave Software, Inc.,
* P.O. Box 2328, Corvallis, Oregon 97339.
*
* This computer software and information is distributed with "restricted
* rights." Use, duplication or disclosure is subject to restrictions as
* set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
* Computer Software-Restricted Rights (April 1985)." If the Clause at
* 18-52.227-74 "Rights in Data General" is specified in the contract,
* then the "Alternate III" clause applies.
*
**************************************************************************/
#include <stdcomp.h>
#include <function>
#include <algorith>
#include <iterator>
#ifndef Allocator
#define Allocator allocator
#include <memory>
#endif
#ifndef list
#define list list
#endif
#ifndef RWSTD_NO_NAMESPACE
namespace std {
#endif
template <class T>
class list
{
protected:
typedef Allocator<void>::pointer void_pointer;
struct list_node;
friend struct list_node;
struct list_node
{
void_pointer next;
void_pointer prev;
T data;
};
Allocator<list_node> list_node_allocator;
Allocator<T> value_allocator;
public:
//
// types
//
typedef T value_type;
typedef Allocator<T> value_allocator_type;
typedef Allocator<T>::pointer pointer;
typedef Allocator<T>::reference reference;
typedef Allocator<T>::const_reference const_reference;
typedef Allocator<list_node> list_node_allocator_type;
typedef Allocator<list_node>::pointer link_type;
typedef Allocator<list_node>::size_type size_type;
typedef Allocator<list_node>::difference_type difference_type;
protected:
static size_type buffer_size ()
{
//
// Each time we allocate memory we reserve space for
// this many nodes. This is currently tuned to
// allocate memory in 1K chunks, except for large objects.
//
return sizeof(list_node) >= 1024 ? 1 : 1024 / sizeof(list_node);
};
struct list_node_buffer;
friend struct list_node_buffer;
struct list_node_buffer
{
void_pointer next_buffer;
link_type buffer;
};
public:
typedef Allocator<list_node_buffer> buffer_allocator_type;
typedef Allocator<list_node_buffer>::pointer buffer_pointer;
protected:
Allocator<list_node_buffer> buffer_allocator;
buffer_pointer buffer_list;
link_type free_list;
link_type next_avail;
link_type last;
void add_new_buffer ()
{
buffer_pointer tmp = buffer_allocator.allocate((size_type)1);
tmp->buffer = list_node_allocator.allocate(buffer_size());
tmp->next_buffer = buffer_list;
buffer_list = tmp;
next_avail = buffer_list->buffer;
last = next_avail + buffer_size();
}
void deallocate_buffers ();
link_type get_node ()
{
link_type tmp = free_list;
return free_list ? (free_list = (link_type)(free_list->next), tmp)
: (next_avail == last ? (add_new_buffer(), next_avail++)
: next_avail++);
}
void put_node (link_type p) { p->next = free_list; free_list = p; }
protected:
link_type node;
size_type length;
public:
class iterator;
class const_iterator;
class iterator : public bidirectional_iterator<T, difference_type>
{
friend class list<T>;
friend class const_iterator;
protected:
link_type node;
iterator (link_type x) : node(x) {}
public:
iterator () {}
bool operator== (const iterator& x) const { return node == x.node; }
reference operator* () const { return (*node).data; }
iterator& operator++ ()
{
node = (link_type)((*node).next); return *this;
}
iterator operator++ (int)
{
iterator tmp = *this; ++*this; return tmp;
}
iterator& operator-- ()
{
node = (link_type)((*node).prev); return *this;
}
iterator operator-- (int)
{
iterator tmp = *this; --*this; return tmp;
}
}; // End of definition of iterator class.
class const_iterator : public bidirectional_iterator<T, difference_type>
{
friend class list<T>;
protected:
link_type node;
const_iterator (link_type x) : node(x) {}
public:
const_iterator () {}
const_iterator (const iterator& x) : node(x.node) {}
bool operator== (const const_iterator& x) const {return node==x.node;}
const_reference operator* () const { return (*node).data; }
const_iterator& operator++ ()
{
node = (link_type)((*node).next); return *this;
}
const_iterator operator++ (int)
{
const_iterator tmp = *this; ++*this; return tmp;
}
const_iterator& operator-- ()
{
node = (link_type)((*node).prev); return *this;
}
const_iterator operator-- (int)
{
const_iterator tmp = *this;
--*this;
return tmp;
}
}; // End of definition of cosnt_iterator class.
typedef reverse_bidirectional_iterator<const_iterator, value_type,
const_reference, difference_type>
const_reverse_iterator;
typedef reverse_bidirectional_iterator<iterator, value_type, reference,
difference_type>
reverse_iterator;